<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.csgrandeur.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.22.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"default"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="贪心 贪心算法是一种在每一步选择中都采取当前状态下最好或最优的选择，从而希望导致结果是最好或最优的算法策略。">
<meta property="og:type" content="article">
<meta property="og:title" content="17.贪心">
<meta property="og:url" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/index.html">
<meta property="og:site_name" content="CSGrandeur&#39;s Thinking">
<meta property="og:description" content="贪心 贪心算法是一种在每一步选择中都采取当前状态下最好或最优的选择，从而希望导致结果是最好或最优的算法策略。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A51%E5%8F%8D%E4%BE%8B.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A52%E5%8F%8D%E4%BE%8B.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A53%E5%BD%92%E7%BA%B3%E5%9F%BA%E7%A1%80.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A53%E5%BD%92%E7%BA%B3%E8%AE%BA%E8%AF%81.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%9C%80%E5%B0%8F%E5%BB%B6%E8%BF%9F%E8%B0%83%E5%BA%A6_%E6%A0%B7%E4%BE%8B%E8%B0%83%E5%BA%A6.svg">
<meta property="og:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%9C%80%E5%B0%8F%E5%BB%B6%E8%BF%9F%E8%B0%83%E5%BA%A6_%E7%AD%96%E7%95%A53%E4%BA%A4%E6%8D%A2%E8%AE%BA%E8%AF%81.svg">
<meta property="article:published_time" content="2025-04-02T08:33:13.000Z">
<meta property="article:modified_time" content="2025-04-03T02:18:43.303Z">
<meta property="article:author" content="CSGrandeur">
<meta property="article:tag" content="ACM">
<meta property="article:tag" content="Algorithm">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A51%E5%8F%8D%E4%BE%8B.svg">


<link rel="canonical" href="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/","path":"2025-04-02-17-贪心/","title":"17.贪心"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>17.贪心 | CSGrandeur's Thinking</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?7958adf931092425a489778560129144"></script>







  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">CSGrandeur's Thinking</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Cogito Ergo Sum</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%B4%AA%E5%BF%83"><span class="nav-number">1.</span> <span class="nav-text">贪心</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%87%8D%E5%9C%A8%E8%AF%81%E6%98%8E"><span class="nav-number">1.1.</span> <span class="nav-text">重在证明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%83%8C%E5%8C%85%E7%B1%BB%E9%97%AE%E9%A2%98"><span class="nav-number">1.2.</span> <span class="nav-text">背包类问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BE%8B%E6%9C%80%E4%BC%98%E8%A3%85%E8%BD%BD"><span class="nav-number">1.2.1.</span> <span class="nav-text">例：最优装载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BE%8B%E5%88%86%E6%95%B0%E8%83%8C%E5%8C%85"><span class="nav-number">1.2.2.</span> <span class="nav-text">例：分数背包</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8C%BA%E9%97%B4%E7%B1%BB%E9%97%AE%E9%A2%98"><span class="nav-number">1.3.</span> <span class="nav-text">区间类问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BE%8B%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9"><span class="nav-number">1.3.1.</span> <span class="nav-text">例：活动选择</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BE%8B%E6%9C%80%E5%B0%8F%E5%BB%B6%E8%BF%9F%E8%B0%83%E5%BA%A6"><span class="nav-number">1.3.2.</span> <span class="nav-text">例：最小延迟调度</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">CSGrandeur</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">72</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://blog.csgrandeur.cn/2025-04-02-17-%E8%B4%AA%E5%BF%83/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="CSGrandeur">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="17.贪心 | CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          17.贪心
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-04-02 16:33:13" itemprop="dateCreated datePublished" datetime="2025-04-02T16:33:13+08:00">2025-04-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-04-03 10:18:43" itemprop="dateModified" datetime="2025-04-03T10:18:43+08:00">2025-04-03</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/" itemprop="url" rel="index"><span itemprop="name">ACM</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/ACMCOURSE/" itemprop="url" rel="index"><span itemprop="name">ACMCOURSE</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="贪心">贪心</h1>
<p>贪心算法是一种在每一步选择中都采取当前状态下最好或最优的选择，从而希望导致结果是最好或最优的算法策略。</p>
<span id="more"></span>
<h2 id="重在证明">重在证明</h2>
<p>局部最优就能得到全局最优，这是一件很美好的事情，使决策变得简单易执行。</p>
<p>当然不是所有问题都能这样得到最优解，所以一个问题是否可以用贪心方式解决，就需要先证明贪心策略的正确性。</p>
<p>否定一个贪心策略很容易，找到一个反面例子。</p>
<p>所以尝试贪心方法的大体思路就是：</p>
<ol type="1">
<li>想一些拍脑门就有的局部最优策略</li>
<li>尝试举反例否定它们</li>
<li>对暂时找不到反例的思路，尝试证明正确性</li>
</ol>
<p>比较典型的证明方法：</p>
<ul>
<li>反证法：作否定假设，则有更优解，进而推出矛盾</li>
<li>归纳法：证<span class="math inline">\(n=1\)</span>成立，假设<span
class="math inline">\(n\)</span>成立，推<span
class="math inline">\(n+1\)</span>成立</li>
<li>交换论证：交换贪心方案的两个元素，答案不会变得更好，或假设任意最优解，能通过等价交换得到贪心的方案</li>
</ul>
<h2 id="背包类问题">背包类问题</h2>
<h3 id="例最优装载">例：最优装载</h3>
<p><span class="math inline">\(n\)</span> 个物体，第 <span
class="math inline">\(i\)</span> 个物体重量<span
class="math inline">\(w_i\)</span>，求总重量不超过<span
class="math inline">\(C\)</span>的最多物体个数</p>
<p>交换论证： 假设存在最优解<span class="math inline">\(w_{s_1} \leq
w_{s_2} \leq \ldots \leq w_{s_k}\)</span>，不是轻者优先 则存在<span
class="math inline">\(w_i \leq w_{s_k}\)</span>，用<span
class="math inline">\(w_i\)</span>替换<span
class="math inline">\(w_{s_k}\)</span>仍然不会超过容量，且个数不变
不断执行该替换，总能得到轻者优先的方案</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> ans = <span class="number">0</span>, tc = C;</span><br><span class="line">std::<span class="built_in">sort</span>(w, w + n);</span><br><span class="line"><span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; n &amp;&amp; tc &gt;= w[i]; tc -= w[i ++], ans ++);</span><br></pre></td></tr></table></figure>
<h3 id="例分数背包">例：分数背包</h3>
<p>有<span class="math inline">\(n\)</span>个物品，每个物品<span
class="math inline">\(i \in [1,n]\)</span>的重量<span
class="math inline">\(w_i\)</span>，价值<span
class="math inline">\(v_i\)</span>（分别只有1个）
有一个背包可装总重不超过<span class="math inline">\(b\)</span>的物品
求背包能装物品的最大总价值
物品可进行任意精度的分割（可以理解为金沙、液体等）</p>
<p>分数背包 贪心策略：“性价比”<span
class="math inline">\(\frac{v_i}{w_i}\)</span> 排序，高者优先</p>
<p>对0-1背包 不适用</p>
<p>举反例：</p>
<p><span class="math inline">\(W=\{3,4,5,2\}\)</span>，<span
class="math inline">\(V=\{7,9,9,2\}\)</span>，<span
class="math inline">\(C=6\)</span></p>
<p><span class="math inline">\(\frac{7}{3} &gt; \frac{9}{4} &gt;
\frac{9}{5} &gt; \frac{2}{2}\)</span></p>
<p>贪心解<span class="math inline">\(3+2=5&lt;6\)</span>，<span
class="math inline">\(7+2=9\)</span></p>
<p>最优解<span class="math inline">\(4+2=6\)</span>，<span
class="math inline">\(9+2=11&gt;9\)</span></p>
<h2 id="区间类问题">区间类问题</h2>
<h3 id="例活动选择">例：活动选择</h3>
<p>给出<span class="math inline">\(n\)</span>项活动的起止时间<span
class="math inline">\([s_i,f_i](i=1,2,\ldots,n)\)</span>，最多选出多少个活动时间互不重叠(<span
class="math inline">\(s_i \geq f_j\)</span> 或 <span
class="math inline">\(f_i \leq s_j\)</span>) 例：</p>
<table>
<thead>
<tr class="header">
<th>i</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><span class="math inline">\(s_{i}\)</span></td>
<td>1</td>
<td>3</td>
<td>5</td>
<td>4</td>
<td>6</td>
<td>8</td>
<td>2</td>
</tr>
<tr class="even">
<td><span class="math inline">\(f_{i}\)</span></td>
<td>4</td>
<td>5</td>
<td>7</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>13</td>
</tr>
</tbody>
</table>
<p><span class="math inline">\(\{1,3,6\}\)</span>是最优解之一</p>
<p>思考：</p>
<ul>
<li>策略1：开始时间早的优先</li>
<li>策略2：占用时间少的优先</li>
<li>策略3：结束早的优先</li>
</ul>
<p>反例：</p>
<p>策略1：<img src="/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A51%E5%8F%8D%E4%BE%8B.svg" class="" title="开始时间早的优先反例"><br />
策略2：<img src="/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A52%E5%8F%8D%E4%BE%8B.svg" class="" title="占用时间少的优先反例"></p>
<p>证明策略3结束早的优先能得到最优解——安排最多的活动</p>
<p>假设活动已按结束时间排序，<span class="math inline">\(f_1 \leq f_2
\leq \ldots \leq f_n\)</span></p>
<p>归纳基础：存在最优解包含可选活动里结束最早的活动。考虑多解情况，换种说法就是选最早结束的活动，不会导致得不到最优解。</p>
<p>交换论证，如果存在最优解不包含活动1，活动1一定可以代替该最优解中结束时间最早的那个活动，仍然是最优解</p>
<img src="/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A53%E5%BD%92%E7%BA%B3%E5%9F%BA%E7%A1%80.svg" class="" title="存在最优解包含可选活动里结束最早的活动">
<p>归纳论证：按此策略选了 <span class="math inline">\(k\)</span>
个活动后，剩下的可选活动里选第 <span class="math inline">\(k+1\)</span>
个活动时，选结束最早的那个，不会导致得不到最优解。</p>
<img src="/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%B4%BB%E5%8A%A8%E9%80%89%E6%8B%A9_%E7%AD%96%E7%95%A53%E5%BD%92%E7%BA%B3%E8%AE%BA%E8%AF%81.svg" class="" title="归纳论证">
<p>图中 <span class="math inline">\(S&#39;\)</span> 是选了 <span
class="math inline">\(k\)</span>
个活动后，剩下的不冲突的可选活动，如果把 <span
class="math inline">\(S&#39;\)</span>
看作一个独立的活动选择问题，这个问题的最优解是 <span
class="math inline">\(B\)</span> 集合，则可以用反证法证明 <span
class="math inline">\(B\)</span> 集合和前面的 <span
class="math inline">\(k\)</span>
个活动合起来，是原来整个问题的最优解。</p>
<blockquote>
<p>为了防止新手绕晕，关于这个需要反证的论断可以先跳过，读完证明后再返回来看：<span
class="math inline">\(S&#39;\)</span> 的最优解 <span
class="math inline">\(B\)</span>，<span class="math inline">\(B \cup
\{i_1,i_2,\ldots,i_k\}\)</span>
是全局最优解，反证法证明它，可以做一个否定的假设——<span
class="math inline">\(S&#39;\)</span> 的最优解 <span
class="math inline">\(B\)</span>，<span class="math inline">\(B \cup
\{i_1,i_2,\ldots,i_k\}\)</span>
<strong>不</strong>是全局最优解，那必然存在 <span
class="math inline">\(S&#39;\)</span> 的另一个子集 <span
class="math inline">\(B*\)</span>，<span class="math inline">\(B* \cup
\{i_1,i_2,\ldots,i_k\}\)</span>是全局最优解（总得有个最优解吧），那<span
class="math inline">\(\{i_1,i_2,\ldots,i_k\}\)</span>都不变，只能是
<span class="math inline">\(B*\)</span> 比 <span
class="math inline">\(B\)</span> 元素多。既然 <span
class="math inline">\(B*\)</span> 也是 <span
class="math inline">\(S&#39;\)</span> 的子集，又比 <span
class="math inline">\(B\)</span> 元素多，那为啥 <span
class="math inline">\(B*\)</span> 不是 <span
class="math inline">\(S&#39;\)</span>
最优解呢，这就得到矛盾了，于是推翻反证法的否定假设，原命题成立。</p>
</blockquote>
<ul>
<li>既然<span
class="math inline">\(B\)</span>是整个问题最优解的一部分，那么第<span
class="math inline">\(k+1\)</span>个活动可以包含在<span
class="math inline">\(B\)</span>中（考虑多解问题，这里用“可以”来表述，仍然表达的是“不会导致得不到最优解”这个含义）</li>
<li>而 <span class="math inline">\(B\)</span> 又同时是 <span
class="math inline">\(S&#39;\)</span>
这个独立问题的最优解，根据归纳基础——存在最优解包含可选活动里结束最早的活动</li>
<li>那么第<span class="math inline">\(k+1\)</span>个活动就可以是<span
class="math inline">\(S&#39;\)</span>结束最早的活动</li>
</ul>
<p>到这里，就说明了，不断选剩下的可选活动里结束最早的活动，能够一步步走向最优解（因为每一步都不会导致得不到最优解）。</p>
<h3 id="例最小延迟调度">例：最小延迟调度</h3>
<p>给出 <span class="math inline">\(n\)</span> 个任务的执行时长 <span
class="math inline">\(t_i\)</span> 与预期截止时间 <span
class="math inline">\(d_i\)</span> <span
class="math inline">\((i=1,2,…,n)\)</span>，求一个调度方案，使超时最严重的任务的超时时长最小。</p>
<p>例：<span class="math inline">\(T=\{5,8,4,10,3\}\)</span>，<span
class="math inline">\(D=\{10,12,15,11,20\}\)</span></p>
<p>调度1：顺序安排 延迟：<span
class="math inline">\(0,1,2,16,10\)</span> 最大延迟：<span
class="math inline">\(16\)</span></p>
<p>调度2：截止时间安排 延迟：<span
class="math inline">\(0,4,11,12,10\)</span> 最大延迟：<span
class="math inline">\(12\)</span></p>
<img src="/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%9C%80%E5%B0%8F%E5%BB%B6%E8%BF%9F%E8%B0%83%E5%BA%A6_%E6%A0%B7%E4%BE%8B%E8%B0%83%E5%BA%A6.svg" class="" title="最小延迟调度_样例调度">
<ul>
<li>策略1：按照 <span class="math inline">\(t_i\)</span>
从小到大安排</li>
<li>策略2：按照 <span class="math inline">\(d_i - t_i\)</span>
从小到大安排</li>
<li>策略3：按照 <span class="math inline">\(d_i\)</span>
从小到大安排</li>
</ul>
<p>反例：</p>
<ul>
<li>策略1: <span class="math inline">\(t_1=1, d_1=100, t_2=10,
d_2=10\)</span></li>
<li>策略2:：<span class="math inline">\(t_1=1, d_1=2, t_2=10,
d_2=10\)</span></li>
</ul>
<p>交换论证证明策略3：按照 <span class="math inline">\(d_i\)</span>
从小到大安排</p>
<p>如果有相邻的<span
class="math inline">\(2\)</span>个任务，前一个比后一个 <span
class="math inline">\(d_i\)</span>
大，交换这两个任务，不会让延迟增加。</p>
<style>.bfzbvysllpcz{width: 400px;}</style>
<img src="/2025-04-02-17-%E8%B4%AA%E5%BF%83/%E6%9C%80%E5%B0%8F%E5%BB%B6%E8%BF%9F%E8%B0%83%E5%BA%A6_%E7%AD%96%E7%95%A53%E4%BA%A4%E6%8D%A2%E8%AE%BA%E8%AF%81.svg" class="bfzbvysllpcz" title="策略3交换论证">

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ACM/" rel="tag"># ACM</a>
              <a href="/tags/Algorithm/" rel="tag"># Algorithm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2025-04-02-16-%E5%89%8D%E7%BC%80%E5%92%8C%E3%80%81%E5%B7%AE%E5%88%86/" rel="prev" title="16.前缀和、差分">
                  <i class="fa fa-angle-left"></i> 16.前缀和、差分
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2025-04-02-18-%E4%BA%8C%E5%88%86%E7%AD%94%E6%A1%88/" rel="next" title="18.二分答案">
                  18.二分答案 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">CSGrandeur</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://fastly.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://fastly.jsdelivr.net/npm/@next-theme/pjax@0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://fastly.jsdelivr.net/npm/hexo-generator-searchdb@1.4.1/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>







  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://fastly.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"CSGrandeur/csgrandeur.github.io","issue_term":"pathname","theme":"github-light"}</script>
<script src="/js/third-party/comments/utterances.js"></script>

</body>
</html>
